<?php
include_once("dbconnect/db.php");
Class Common
{
	var $carrierId = "";
	
	var $uId = "";
	
	// "1":docomo;"2":au;"3":softbank
	function getCarrierType()
	{
		$userAgent = $_SERVER["HTTP_USER_AGENT"];
		
		$carrierType = "";
		
        if (strpos($userAgent,"KDDI") !== false || 
        	strpos($userAgent,"UP.Browser") !== false)
		{
		    // au
		    $carrierType = "2";
		}
		else if (strpos($userAgent,"DoCoMo") !== false || 
				strpos($userAgent,"L-mode") !== false || 
				strpos($userAgent,"ASTEL") !== false)
		{
		    // docomo
		    $carrierType = "1";
		}
		else if (strpos($userAgent,"J-PHONE") !== false || 
				strpos($userAgent,"Vodafone") !== false || 
				strpos($userAgent,"MOT") !== false || 
				strpos($userAgent,"SoftBank") !== false)
		{
		    // softbank
		    $carrierType = "3";
		}

		return $carrierType;
	}
	
	function getUidDocomo()
	{
        $uId = "";
        $uId = $_GET["UID"];
        if ($uId == null || $uId == "" || $uId == "NULLGWDOCOMO")
        {
            $uId = "NULLGWDOCOMO";
        }

        if (($uId != null && $uId != "")
            && strlen($uId) > 10 
            && $uId != "NULLGWDOCOMO")
            $uId = substr($uId,strlen($uId) - 10, 10);

        return $uId;		
	}	
	
	function getUidAu()
    {
        $uId = "";
        $uId = $_SERVER["HTTP_X_UP_SUBNO"];

        return $uId;
    }
    
    function getUidSoftBank()
	{
	    $uId = "";
	    preg_match("/^.+\/SN([0-9a-zA-Z]+).*$/",$_SERVER["HTTP_USER_AGENT"], $match);
		$uId = $match[1];
	    return $uId;
	}
	
	function initCarrierInfo()
	{
		$this->carrierId = $this->getCarrierType();
		// au､ﾎ因ｺﾏ
		if($this->carrierId == "2")
		{
			$this->uId = $this->getUidAu();
		}
		// docomo､ﾎ因ｺﾏ
		else if($this->carrierId == "1")
		{
			$this->uId = $this->getUidDocomo();
		}
		// softbank､ﾎ因ｺﾏ
		else if($this->carrierId == "3")
		{
			$this->uId = $this->getUidSoftBank();
		}	
		// PC､ﾎ因ｺﾏ
		else 
		{
			//header("Location:../douga/");
		}
	}
	
	static public function getThumbByCID($cid)
	{
		return "http://i1.ytimg.com/vi/$cid/default.jpg";
	}
	
	static public function getRandomAd($type_c=0)
	{
		$html = '<hr color="#3399FF">';
		$sql = "select adspace_id,ad_name,tag_contents,core_link,type_c
		       from adspace_mst 
			   where type = 2 and open_flag = 1 and type_c=$type_c";
		$resultAd = mysql_query($sql);
		$rowsAd = mysql_num_rows($resultAd);
		
		if($rowsAd == 0 ) return $html;
		$rand = rand(0, $rowsAd-1);
		// add by 費 begin
		if($type_c != 0)
		{
			$html = "";
			for ($i=0;$i<$rowsAd;$i++)
			{
				$row = mysql_fetch_array($resultAd);
				$tag = $row[2];
				$link = $row[3];
				$redLink = "redirect.php?uid=NULLGWDOCOMO&adid=$row[0]&type_c=$row[4]";
				$redirectTag = str_ireplace('"'.$link.'"', '"'.$redLink.'"', $tag);
				$html .= '<hr color="#3399FF">'.$redirectTag;
			}	
			
			return $html.'<hr color="#3399FF">';		
		}
		
		else
		{
			for ($i=0;$i<$rowsAd;$i++)
			{
				$row = mysql_fetch_array($resultAd);
				if($i == $rand)
				{
					$tag = $row[2];
					$link = $row[3];
					$redLink = "redirect.php?uid=NULLGWDOCOMO&adid=$row[0]&type_c=$row[4]";
					$redirectTag = str_ireplace('"'.$link.'"', '"'.$redLink.'"', $tag);
					$html .= $redirectTag.'<hr color="#3399FF">';
					return $html;
				}
			}			
		}
		
		return $html;
	}

}
?>